// pages/Album/NewAlbum.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
   // CoverImage:'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497851227078&di=6913a1634b8f195b5d90aad062615259&imgtype=0&src=http%3A%2F%2Fphotocdn.sohu.com%2F20131030%2FImg389248920.jpg',
    CoverImage:'/images/system/book.png',
    scrollHeight:0,
    ChooseList:[],
    pic_count:0
  },

  UpFile(){
    if(!this.data.set_album_name){
      wx.showToast({
        title:'请先设置相册名!',
      })
      return
    }
    var t = this
    wx.chooseImage({
      count:9,
      success: function(res) {
        wx.showLoading({
          title:'图片正在上传中...',
        })
        t.UpPic(t,res.tempFilePaths)
      }
    })
  },

  UpPic(t,path){
    if(t.data.pic_count < path.length){
      wx.uploadFile({
        url: app.globalData.urls, 
        filePath:path[t.data.pic_count],
        name: 'file',
        formData:{
          'GetTypes':'Picture_Add',
          'album_id':t.data.aid,
          'oid':wx.getStorageSync('openid')
        },
        complete: function(res){
        //  console.log(res.data)
          t.setData({
            pic_count:t.data.pic_count+1
          })
          if(res.data == 'ok'){
            t.UpPic(t,path)
          }
        }
      })
    }else{
      t.setData({
        pic_count:0
      })
      wx.showToast({
        title:'上传完成！'
      })
      this.GetPics(t.data.aid,1);
    }
  },



  //获取输入的相册名
  set_album_name(e){
    var name = e.detail.value.trim();
    if(name){
      this.setData({
        set_album_name:e.detail.value.trim()
      })
    }
  },


  sub_album_name(e){
    var t = this
    var album_name = e.detail.value.trim()
    if(!album_name){
      return
    }
    var openid = wx.getStorageSync('openid')


    if(t.data.aid){
      var data = {GetTypes:'Album_Change_Info',Album_Id:this.data.aid,Album_Name:album_name}
    }else{
      var data = {GetTypes:'Album_Add',Album_UserOpenid:openid,Album_Name:album_name}
    }
    wx.request({
      url:app.globalData.urls,
      data:data,
      success:function(res){
        if(isNaN(res.data) && res.data=='ok'){
          console.log('更新成功')
        }
        if(res.data > 0){
          t.setData({
            aid:res.data
          })
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.cover > 0){
      var cover = options.cover;
    }else{
      var cover = "null";
    }
    this.setData({
      aid:options.aid,
      set_album_name:options.aname,
      cover:cover,
      domain:app.globalData.domain     
    })
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        })
      }
    })
    if(options.aid){
      this.GetPics(this.data.aid)
    }
  },


  //获取相册列表 

  GetPics(album_id){
    var t = this
    wx.request({
      url:app.globalData.urls,
      data:{
        'GetTypes':'Get_All_Pics',
        'album_id':album_id
      },
      success:function(res){
        for(var i=0;i<res.data.length;i++){
          if(res.data[i].Picture_Id == t.data.cover){
            t.setData({
              cover_url:app.globalData.domain+res.data[i].Picture_Src
            })
            break;
          }
        }
        if(res.data=='err'){
          return
        }else{
          t.setData({
            pics:res.data
          })
        }
        wx.setStorageSync('public_pics',res.data);
      //  console.log(wx.getStorageSync('public_pics'))
      }
    })
  },



  onChooseClick:function(){
    if(!this.data.set_album_name){
      wx.showToast({
        title:'请先设置相册名!',
      })
      return
    }
    var imgs = this.data.ChooseList;
    var t=this;
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        for (var i = 0; i < res.tempFilePaths.length;i++){
          //执行上传完成后执行添加数组
          imgs.push(res.tempFilePaths[i]);
        }
        t.setData({
          ChooseList: imgs
        });
      }
    })
  },

  //设置封面
  onChooseCoverClick: function (e) {
    //更新数据后执行
    var t = this
    var pid = e.currentTarget.dataset.pid;
    var url = e.currentTarget.dataset.url;
    wx.request({
      url:app.globalData.urls,
      data:{
        'GetTypes':'Album_Cover_Change',
        'aid':t.data.aid,
        "pid":pid
      },
      success:function(res){
        if(res.data == 'ok'){
          wx.showToast({
            title:'设置成功！',
            success:function(){
              t.setData({
                cover:pid,
                cover_url:url
              })
            }
          })
        }else{
          wx.showToast({
            title:'设置失败！',
          })
        }
      }
    });

  },

  //图片的删除
  Img_Del(e){
    var t = this
    var del_pid = e.currentTarget.dataset.del_pid;
    wx.request({
      url:app.globalData.urls,
      data:{
        'GetTypes':'Picture_Del',
        "pid":del_pid
      },
      success:function(res){
        t.GetPics(t.data.aid)
      }
    })
  }


})